<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
      <el-form-item label="产品名称" prop="manufacturer">
        <el-input v-model="dataForm.deviceDesc" placeholder="产品名称"></el-input>
      </el-form-item>
      <el-form-item label="产品型号" prop="deviceModel">
        <el-input v-model="dataForm.deviceModel" placeholder="产品型号"></el-input>
      </el-form-item>
      <el-form-item label="产品厂商" prop="deviceDesc">
        <el-input v-model="dataForm.manufacturer" placeholder="产品厂商"></el-input>
      </el-form-item>
      <el-form-item label="协议" prop="protocol">
        <el-select v-model="dataForm.protocol" placeholder="协议" size="large" style="width: 240px">
          <el-option v-for="item in ProtocolData" :key="item.id" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品类型" prop="attributes">
        <el-radio-group v-model="dataForm.deviceType">
          <el-radio :value="0">有线</el-radio>
          <el-radio :value="1">无线</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="支持功能" prop="functions">
        <el-checkbox-group v-model="dataForm.functions">
          <el-checkbox v-for="item in AllFuncs" :key="item.id" :label="item[1]" :value="item[0]" />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="设备号正则" prop="reg">
        <el-input v-model="dataForm.reg" placeholder="设备号正则式"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { getProtocol, useProductSubmitApi, getAllFuncs, useProductApi } from '@/api/gps/product'
const emit = defineEmits(['refreshDataList'])
const ProtocolData = ref()
const AllFuncs = ref()
const visible = ref(false)
const dataFormRef = ref()
const dataForm = reactive({
  id: '',
  manufacturer: '',
  deviceModel: '',
  deviceDesc: '',
  protocol: '',
  protDef: '',
  deviceType: 1,
  attributes: null,
  functions: [],
  reg: ''
})

//获取协议&
function Protocol() {
  getProtocol().then((res) => {
    ProtocolData.value = res.data
    console.log(ProtocolData.value[0])
  })
  getAllFuncs().then((res) => {
    console.log(res)
    AllFuncs.value = Object.entries(res.data)
    console.log(AllFuncs.value)
  })
}

const init = (id?: number) => {
  visible.value = true
  dataForm.id = ''
  Protocol()
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields()
  }
  if (id) {
    getProduct(id)
  }
}

const getProduct = (id: number) => {
  useProductApi(id).then((res) => {
    Object.assign(dataForm, res.data)
  })
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }

    useProductSubmitApi(dataForm).then(() => {
      ElMessage.success({
        message: '操作成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}

defineExpose({
  init
})
</script>
